<?php

require_once 'src/core/Servicos.php';
$s = new Servicos();

$error = null;

if (!isset($_POST['idservico'])) {
    $error['form'] = "não existe";
}

/**
 * Deletar
 */
if (isset($_GET['del'])) {
    $s->setIdservico($_GET['del']);
    $s->delete();
}

/**
 * Inserir
 */
if (isset($_GET['adicionar'])) {
//    echo "<script>alert('Cliente adicionado com sucesso!');</script>";
    if (sizeof($error) == 0) {
        $s->setObjServico(null, $_POST['nome'], $_POST['descricao'], $_POST['precobase'], $_POST['desconto'], ($_POST['isPromocao'] ? "1" : "0"));
        $s->insert();
        header("Location: ?s=cservico&msg=1");
    } else {
        $link_form = "?s=cservico&adicionar";
        include 'src/view/from_servico.php';
    }
}

/**
 * Editar
 */
if (isset($_GET['edit'])) {
    if (sizeof($error) == 0) {
        $s->setObjServico($_POST['idservico'], $_POST['nome'], $_POST['descricao'], $_POST['precobase'], $_POST['desconto'], ($_POST['isPromocao'] ? "1" : "0"));
        $s->update();
        header("Location: ?s=cservico&msg=2");
    } else {
        $s->setIdservico($_GET['edit']);
        $s->load();
        $link_form = "?s=cservico&edit";
        include 'src/view/from_servico.php';
    }
}
/**
 * Listar
 */
$lista = $s->select('', '');
//print_r($lista);

include 'src/view/servico.php';
?>